package com.hzhr.iot.collect.protocol.responder;

import com.hzhr.iot.collect.constant.MessageFieldKeyEnum;
import com.hzhr.iot.collect.constant.UpDownEnum;
import com.hzhr.iot.collect.constant.YesOrNoEnum;
import com.hzhr.iot.collect.constant.entity.FunctionEntity;
import com.hzhr.iot.collect.constant.entity.MessagetmpEntity;
import com.hzhr.iot.collect.constant.entity.constant.FunctionEntityConstant;
import com.hzhr.iot.collect.constant.entity.constant.MessagetmpEntityConstant;
import com.hzhr.iot.collect.dao.helper.CachedCommandDaoHelper;
import com.hzhr.iot.collect.entity.CachedCommandEntity;
import com.hzhr.iot.collect.model.CommandResponseModel;
import com.hzhr.iot.collect.protocol.logger.Logger;
import com.hzhr.iot.collect.protocol.parser.Parser;
import com.hzhr.iot.collect.protocol.sender.Sender;
import com.hzhr.iot.collect.protocol.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class Responder3 implements Responder {
    @Autowired
    CachedCommandDaoHelper cachedCommandDaoHelper;
    @Autowired
    MessageUtil messageUtil;
    @Autowired
    Sender sender1;

    @Override
    public Sender getSender() {
        return sender1;
    }

    @Override
    public CommandResponseModel response(List<String> respMsgList, String deviceId, int protocolId, String functionCode, Parser parser) throws Exception {
        FunctionEntity functionEntity = FunctionEntityConstant.getFunctionEntityByProtocolIdAndFunctionCode(protocolId, functionCode);
        CommandResponseModel commandResponseModel = new CommandResponseModel();

        if (respMsgList == null || respMsgList.size() == 0) {
            return commandResponseModel;
        }

        //响应
        Integer iswaitresp = functionEntity.getIswaitresp();
        if (iswaitresp != null && iswaitresp.equals(YesOrNoEnum.YES.getId())) {//如果需要等待响应 则需等待缓存池中的指令处理完成才能响应
            //获取缓存池中的指令
            List<CachedCommandEntity> commandModelListInPool = cachedCommandDaoHelper.getListByDeviceId(deviceId);

            if (commandModelListInPool == null || commandModelListInPool.size() == 0) {//如果缓存池中没有指令，直接返回上行指令的响应
                commandResponseModel.setDeviceId(deviceId);
                commandResponseModel.setSenderId(1);
                commandResponseModel.setExpireTime(2);
                commandResponseModel.setRespMsgList(respMsgList);
            } else {//如果缓存池中没有指令，直接返回上行指令的响应
                List<String> respMsg = new ArrayList<>();
                for (CachedCommandEntity cachedCommandEntity :
                commandModelListInPool) {
                    Map<MessageFieldKeyEnum, Object> messageObj = cachedCommandEntity.getMessageObj();
                    String iotDeviceCode = messageObj.get(MessageFieldKeyEnum.IOT_DEVICE_ID).toString();
                    String functionCode1 = messageObj.get(MessageFieldKeyEnum.FUNCTION_CODE).toString();//功能命令的功能码

                    Map<MessageFieldKeyEnum, Object> reqCommandContent = new HashMap<>();
                    reqCommandContent.put(MessageFieldKeyEnum.REQ_FUNCTION_CODE, functionCode1);
                    Map<MessageFieldKeyEnum, Object> reqCommandMessageObj = messageUtil.generatePressureMonitorReq(protocolId, "10",
                            iotDeviceCode, messageUtil.getNextMessageId(protocolId, deviceId), reqCommandContent);//请求更新命令
                    String reqCommandMessage = parser.inverselyParsing(reqCommandMessageObj, protocolId, deviceId, UpDownEnum.DOWN);

                    respMsg.add(reqCommandMessage);
                    respMsg.add(cachedCommandEntity.getCommand());
                }
                respMsg.addAll(respMsgList);

                commandResponseModel.setDeviceId(deviceId);
                commandResponseModel.setSenderId(1);
                commandResponseModel.setExpireTime(2);
                commandResponseModel.setRespMsgList(respMsg);
            }

        } else {//如果不需要等待响应 直接响应即可
            commandResponseModel.setDeviceId(deviceId);
            commandResponseModel.setSenderId(1);
            commandResponseModel.setExpireTime(2);
            commandResponseModel.setRespMsgList(respMsgList);
        }

        return commandResponseModel;
    }

}
